package com.youlu.campus.web.community.controller;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.ActivityManualFeedInfo;
import com.youlu.campus.service.activity.ActivityManualFeedInfoService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
public class ActivityFeedController {
    @Autowired
    private ActivityManualFeedInfoService activityManualFeedInfoService;

    @PostMapping("/mini-app/activity/feed-info/pay")
    public QResult list(@RequestBody HashMap<String, Object> req) {
        log.info(":>>> 开始获取活动收款Feed流:{}", JSON.toJSON(req));
        String activityId = (String) req.get("activityId");
        Integer page = (Integer) req.get("page");
        String userId = RequestContext.getUserId();
        Long total = activityManualFeedInfoService.total(activityId);
        Double p = Math.ceil(total / 10);
        Integer pages = p.intValue();
        Integer lastPage = activityManualFeedInfoService.getVisitorFeedPage(userId, activityId);
        log.info(":>>> 活动:{} feed流总页数:{},当前页:{},总数量:{}", activityId, pages, page, total);
        if (Objects.isNull(lastPage)) {
            log.info(":>>> 用户:{} 活动:{} 无访问记录",userId,activityId);
            page = 1;
        } else if ((lastPage.intValue() + 1) == pages.intValue() || lastPage.intValue() == pages.intValue()) {
            log.info(":>>> 用户:{} 活动:{} 访问记录已经是最后两页",userId,activityId);
            page = 1;
        } else {
            page = lastPage++;
        }
        List<ActivityManualFeedInfo> datas = activityManualFeedInfoService.getFeedInfo((String) req.get("activityId"),
                page);
        activityManualFeedInfoService.setVisitorFeedPage(userId, activityId, page);
        return QResult.success(datas);
    }
}
